package com.eascs.admin.o2o.internal.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.badloan.entity.O2oCacleEntity;
import com.eascs.admin.o2o.badloan.service.O2oCancleServiceImpl;
import com.eascs.admin.o2o.internal.entity.WfTypeStatus;
import com.eascs.admin.o2o.internal.form.WfTypeStatusForm;
import com.eascs.admin.o2o.internal.form.WfTypeStatusList;
import com.eascs.admin.o2o.internal.service.WfTypeStatusService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.loan.entity.O2oPaymentsPlan;
import com.eascs.loan.service.O2oPaymentsService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;

/**
 * 工作流 ---状态图
 * 
 * @author ycz
 *
 */
@Controller
@RequestMapping("/wftypeStatus")
public class WfTypeStatusController extends BaseRestController {

	@Autowired
	private WfTypeStatusService wfTypeStatusService;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private O2oPaymentsService o2oPaymentsService; 
	@Autowired
	private O2oCancleServiceImpl o2oCancleServiceImpl; 

	/**
	 *终端贷---贷前节点状态图
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	@RequestMapping("/bk/beforeStatus")
	public HtmlRestResult bKworkFlowStatus(String loanapplyNo) {
		List<WfTypeStatus> wfTypelist = wfTypeStatusService.queryWfTypeStatus(loanapplyNo);
		O2oLoanapplyEntity loanpply = o2oLoanapplyService.findById(loanapplyNo);
		List<WfTypeStatusForm> wflist = new ArrayList<WfTypeStatusForm>();
		if((StatusContexts.O2O_FK_OVER).equals(loanpply.getWfType())){
			WfTypeStatusForm wf = new WfTypeStatusForm();
			O2oCacleEntity cacleEntity = o2oCancleServiceImpl.findByLoanapplyId(loanapplyNo);
			if (loanpply.getCreditstatus()==0) {
				wf.setTitle(StatusContexts.ZF_TITILE+StatusContexts.ZF_ZXSX);
			}else if(null!=cacleEntity && !StringUtils.isEmpty(cacleEntity.gettVetoTerm())){
				wf.setTitle(StatusContexts.ZF_TITILE+"(否决作废)");
			}else{
				wf.setTitle(StatusContexts.ZF_TITILE);
			}
			wf.setStyle("red");
			wf.setTime(loanpply.getEndDate());
			wflist.add(wf);
		}else{
		for (int i = 0; i < 6; i++) {
			WfTypeStatusForm wf = new WfTypeStatusForm();
			if (i == 0) {
				wf.setTitle(StatusContexts.BEGIN_TITILE);
				wf.setStyle("hot");
				wf.setTime(loanpply.getCreateDate());
			}
			if (i == 1) {
				wf.setTitle(StatusContexts.ZX_TITILE);
			}
			if (i == 2) {
				wf.setTitle(StatusContexts.NS_TITILE);
			}
			if (i == 3) {
				wf.setTitle(StatusContexts.BZJ_TITILE);
			}
			if (i == 4) {
				wf.setTitle(StatusContexts.SB_TITILE);
			}
			if (i == 5) {
				if(StatusContexts.O2O_SB_FAIL.equals(loanpply.getWfType())){
						wf.setTitle(StatusContexts.FK_FAIL_TITLE);
						wf.setTime(loanpply.getEndDate());
						wf.setStyle("hot");
				}else{
					wf.setTitle(StatusContexts.FK_TITILE);
					if(loanpply.getPaymentTime()!=null){
						wf.setTime(loanpply.getPaymentTime());
						wf.setStyle("hot");
					}	
				}
			}
			wflist.add(wf);
		}
		for (WfTypeStatus wfTypeStatus : wfTypelist) {
			WfTypeStatusForm wf = null;
			if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_ZX)) {
				wf = wflist.get(1);
			}
			if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_NS)) {
				wf = wflist.get(2);
			}
			if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_BZJ)) {
				wf = wflist.get(3);
			}
			if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_BRT)) {
				wf = wflist.get(4);
			}
			if (wf == null) {
				continue;
			}
			if (wfTypeStatus.getEndDate() == null) {
				wf.setTime(wfTypeStatus.getStartDate());
				wf.setSubhead(StatusContexts.RUN_SUBHEAD_STATUS);
				wf.setStyle("on");
			} else {
				wf.setTime(wfTypeStatus.getEndDate());
				wf.setSubhead(StatusContexts.END_SUBHEAD_STATUS);
				wf.setStyle("hot");
			}
		}
		}
		ViewContext.put("loanpply", loanpply);
		ViewContext.put("wfTypelist", wflist);
		return new HtmlRestResult("detail/beforeStatus");
	}
	/**
	 *接力贷和分期贷---贷前节点状态图
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	@RequestMapping("/oth/beforeStatus")
	public HtmlRestResult workFlowStatus(String loanapplyNo) {
		List<WfTypeStatus> wfTypelist = wfTypeStatusService.queryWfTypeStatus(loanapplyNo);
		O2oLoanapplyEntity loanpply = o2oLoanapplyService.findById(loanapplyNo);
		List<WfTypeStatusForm> wflist = new ArrayList<WfTypeStatusForm>();
		if((StatusContexts.O2O_FK_OVER).equals(loanpply.getWfType())){
			WfTypeStatusForm wf = new WfTypeStatusForm();
			wf.setTitle(StatusContexts.ZF_TITILE);
			wf.setStyle("red");
			wf.setTime(loanpply.getEndDate());
			wflist.add(wf);
		}else{
		for (int i = 0; i < 5; i++) {
			WfTypeStatusForm wf = new WfTypeStatusForm();
			if (i == 0) {
				wf.setTitle(StatusContexts.BEGIN_TITILE);
				wf.setStyle("hot");
				wf.setTime(loanpply.getCreateDate());
			}
			if (i == 1) {
				wf.setTitle(StatusContexts.ZX_TITILE);
			}
			if (i == 2) {
				wf.setTitle(StatusContexts.NS_TITILE);
			}
			if (i == 3) {
				wf.setTitle(StatusContexts.BZJ_TITILE);
			}
			if (i == 4) {
				if(StatusContexts.O2O_SB_FAIL.equals(loanpply.getWfType())){
						wf.setTitle(StatusContexts.FK_FAIL_TITLE);
						wf.setTime(loanpply.getEndDate());
						wf.setStyle("hot");
				}else{
					wf.setTitle(StatusContexts.FK_TITILE);
					if(loanpply.getPaymentTime()!=null){
						wf.setTime(loanpply.getPaymentTime());
						wf.setStyle("hot");
					}	
				}
			}
			wflist.add(wf);
		}
		for (WfTypeStatus wfTypeStatus : wfTypelist) {
			WfTypeStatusForm wf = null;
			if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_ZX)) {
				wf = wflist.get(1);
			}
			if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_NS)) {
				wf = wflist.get(2);
			}
			if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_BZJ)) {
				wf = wflist.get(3);
			}
			if (wf == null) {
				continue;
			}
			if (wfTypeStatus.getEndDate() == null) {
				wf.setTime(wfTypeStatus.getStartDate());
				wf.setSubhead(StatusContexts.RUN_SUBHEAD_STATUS);
				wf.setStyle("on");
			} else {
				wf.setTime(wfTypeStatus.getEndDate());
				wf.setSubhead(StatusContexts.END_SUBHEAD_STATUS);
				wf.setStyle("hot");
			}
		}
		}
		ViewContext.put("loanpply", loanpply);
		ViewContext.put("wfTypelist", wflist);
		return new HtmlRestResult("detail/beforeStatus");
	}
	/**
	 * 贷后 节点状态图
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	@RequestMapping("/afterStatus")
	public HtmlRestResult afterStatus(String loanapplyNo) {
		List<WfTypeStatus> wfTypelist = wfTypeStatusService.queryWfTypeStatus(loanapplyNo);
		O2oLoanapplyEntity loanpply = o2oLoanapplyService.findById(loanapplyNo);
		List<WfTypeStatusForm> wflist = new ArrayList<WfTypeStatusForm>();
		for (int i = 0; i < 4; i++) {
			WfTypeStatusForm wf = new WfTypeStatusForm();
			if (i == 0) {
				wf.setTitle(StatusContexts.FK_TIME_TITILE);
				if(loanpply.getPaymentTime()!=null){
					wf.setSubhead(StatusContexts.END_SUBHEAD_STATUS);
					wf.setTime(loanpply.getPaymentTime());
					wf.setStyle("hot");
				}
			}
			if (i == 1) {
				wf.setTitle(StatusContexts.CONTRACT_TITLE);
			}
			if (i == 2) {
				wf.setTitle(StatusContexts.PAY_REPAY_TITLE);
				List<O2oPaymentsPlan> paymentlist=o2oPaymentsService.queryBillCode(loanpply.getBillCode());
				//int count=o2oPaymentsService.findCount(loanpply.getBillCode(), "JQ");
				if(!paymentlist.isEmpty() && paymentlist.size()>0){
					
//					int size=paymentlist.size()+1;
//					if(size==count){
					if(StatusContexts.O2O_REPAY_END.equals(loanpply.getWfType())){
						wf.setSubhead(StatusContexts.END_SUBHEAD_STATUS);
						wf.setTime(loanpply.getSettleDate());
						//wf.setTime(paymentlist.get(paymentlist.size()-1).getPaymentsDate());
						wf.setStyle("hot");
					}else{
						wf.setSubhead(StatusContexts.RUN_SUBHEAD_STATUS);
						wf.setTime(paymentlist.get(0).getPaymentsDate());
						wf.setStyle("on");
					}
				}
			}
			if (i == 3) {
				wf.setTitle(StatusContexts.COM_BANK_TITLE);
			}
			wflist.add(wf);
		}
		for (WfTypeStatus wfTypeStatus : wfTypelist) {
			WfTypeStatusForm wf = null;
			if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_TYPE_PLACE_ON_FILE)) {
				wf = wflist.get(1);
			}
			if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_BACKFEE)) {
				wf = wflist.get(3);
			}
			if (wf == null) {
				continue;
			}
			if (wfTypeStatus.getEndDate() == null) {
				wf.setTime(wfTypeStatus.getStartDate());
				wf.setSubhead(StatusContexts.RUN_SUBHEAD_STATUS);
				wf.setStyle("on");
			} else {
				wf.setTime(wfTypeStatus.getEndDate());
				wf.setSubhead(StatusContexts.END_SUBHEAD_STATUS);
				wf.setStyle("hot");
			}
		}
		ViewContext.put("loanpply", loanpply);
		ViewContext.put("wfTypelist", wflist);
		return new HtmlRestResult("detail/afterStatus");
	}
	/**
	 * 查看全部   状态列表
	 * @param loanapplyNo
	 * @return
	 */
	@RequestMapping("/list")
	public HtmlRestResult querylist(String loanapplyNo){
		List<WfTypeStatus> wfTypelist = wfTypeStatusService.queryWfTypeStatus(loanapplyNo);
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findById(loanapplyNo);
		List<WfTypeStatusList> wflist=new ArrayList<WfTypeStatusList>();
		WfTypeStatusList wf=new WfTypeStatusList();
		wf.setBeginTime(loanapply.getCreateDate());
		wf.setEndTime(loanapply.getCreateDate());
		wf.setWorkTitle(StatusContexts.BEGIN_TITILE);
		wf.setStatus(StatusContexts.END_SUBHEAD_STATUS);
		wflist.add(wf);
		if(!wfTypelist.isEmpty()&& wfTypelist.size()>0){
			for (WfTypeStatus wfTypeStatus : wfTypelist) {
				 wf = new WfTypeStatusList();
				 wf.setBeginTime(wfTypeStatus.getStartDate());
					wf.setEndTime(wfTypeStatus.getEndDate());
					if(StatusContexts.BZJ_STATUS_RUN.equals(wfTypeStatus.getWfStatus())){
						wf.setStatus(StatusContexts.RUN_SUBHEAD_STATUS);
					}else{
						wf.setStatus(StatusContexts.END_SUBHEAD_STATUS);
					}
				if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_ZX)) {
					wf.setWorkTitle(StatusContexts.ZX_TITILE);
				}
				if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_NS)) {
					wf.setWorkTitle(StatusContexts.NS_TITILE);
				}
				if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_BZJ)) {
					wf.setWorkTitle(StatusContexts.BZJ_TITILE);
				}
				if (wfTypeStatus.getWfType().equals(StatusContexts.WFTYPE_BRT)) {
					wf.setWorkTitle(StatusContexts.SB_TITILE);
				}
				wflist.add(wf);
			}
			if(StatusContexts.O2O_SB_FAIL.equals(loanapply.getWfType())){
				wf=new WfTypeStatusList();
				wf.setWorkTitle(StatusContexts.FK_FAIL_TITLE);
				wf.setEndTime(loanapply.getEndDate());
				wf.setStatus(StatusContexts.END_SUBHEAD_STATUS);
				wflist.add(wf);
			}
			if(StatusContexts.O2O_FK_END.equals(loanapply.getWfType())){
				wf=new WfTypeStatusList();
				wf.setWorkTitle(StatusContexts.FK_TITILE);
				wf.setEndTime(loanapply.getPaymentTime());
				wf.setStatus(StatusContexts.END_SUBHEAD_STATUS);
				wflist.add(wf);
			}
		}
		ViewContext.put("wflist", wflist);
		return HtmlRestResult.popHtmlFragment("detail/workListStatus");
	}
	
}
